package com.字符串;

import java.util.Arrays;

/**
 * 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ，使得从1 到 n 的 min(ai, bi) 总和最大。
 *
 * 示例 1:
 *
 * 输入: [1,4,3,2]
 *
 * 输出: 4
 * 解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4).
 */
public class 数组拆分1 {
    static class Solution {
        /**
         * 先将数组进行排序，然后找出奇数位进行求和
         * @param nums
         * @return
         */
        public int arrayPairSum(int[] nums) {
            int sum = 0;
           Arrays.sort(nums);
            for(int i = 0;i<nums.length;i+=2){
                sum += nums[i];
            }
            return sum;
        }

        /**
         * 冒泡排序
         * 将两个数字相比，每次比出最大的数放到数组的末尾。
         * https://blog.csdn.net/qq_41679818/article/details/90296399
         * @param nums
         */
        public void sort(int[] nums){
            for(int i = 0;i<nums.length-1;i++){
                for(int j = 0;j<nums.length-1-i;j++){
                    if(nums[j] > nums[j+1]){
                        int tmp = nums[j];
                        nums[j] = nums[j+1];
                        nums[j+1] = tmp;
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.arrayPairSum(new int[]{558,-6109,-3171,-8329,1811,2416,4877,-827,4602,4717,-2402,-678,3627,-2391,4361,-780,-4355,2711,-299,-4580,-86,-5647,3371,-1841,4601,8712,3605,4489,1891,6898,-4975,-6088,-3130,1254,-4319,-4531,-8275,-1156,-7398,-6373,-771,-1597,509,-1672,6860,993,3760,1580,2213,-2788,3827,2279,7171,-7211,-3588,-2268,5254,-2535,5669,-4524,-7947,554,3835,2332,-3720,-9232,5315,-3378,1284,2051,4228,6699,2462,7766,-1699,3240,2245,-219,-2120,-6989,2490,3467,3232,-1966,-686,6782,-1279,4566,5854,-4780,-3742,-1790,-1636,-1795,-4103,2172,1890,-3163,-6386,536,-699,-2615,5797,-9581,-6881,-1077,3708,-489,-819,6928,-600,5501,-764,2475,-1978,-8033,4074,-4689,213,5831,-2620,-1290,-6431,-640,-3724,-5794,805,1041,4803,-8482,9539,-4326,-215,-5678,-7262,1359,-6146,-156,-1390,3211,502,6883,1497,-4192,-1050,-5660,3153,7230,4265,2163,-1505,-2230,1514,-5945,-4393,-5076,-7227,-2271,-1407,-3314,-4501,-2487,-2946,7665,2152,-4235,2151,4611,3898,-2213,-971,-7313,4828,4701,-3107,-7662,-1165,2000,-1248,4744,1767,5325,-9323,-1788,-1844,5406,-2935,-562,2056,-2005,-2185,918,4834,-6692,-6013,-2254,-422,2284,-167,-9263,1987,-853,-661,4703,-5,1078,739,3989,-1254,421,-3396,592,-3989,2097,3022,-5987,-6283,1514,5923,1505,-4439,1421,-6945,-4473,2038,-3211,157,5551,-3776,-3638,-4063,992,2621,2,6208,3290,3249,3203,-2777,-7529,-3917,973,-1906,6776,-185,5725,8125,-2027,-3671,8037,-2060,-5453,2266,-3798,-133,2944,-1181,2158,-1321,1606,7916,5226,1197}));
    }

}
